package com.guchenbo.example.lock.reentrant;

/**
 * Created by CreGu on 2016/6/18.
 */
public class SpinLockTest {
    static SpinLock lock = new SpinLock();

    public static void main(String[] args) {


        for (int i = 0; i < 5; i++) {
            Thread thread = new SpinThread("thread-" + i);
            thread.start();
        }
    }

    static class SpinThread extends Thread {

        public SpinThread(String name) {
            super(name);
        }

        /**
         * 获取锁的标准写法
         * <p/>
         * try {
         * lock.lock();
         * doSomething...
         * } finally {
         * lock.unlock();
         * }
         */
        @Override
        public void run() {
            try {
                lock.lock();

                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

}
